import java.util.Random;

//************************************************************
//	COPYRIGHT 2011 Bread Zeppelin - ALL RIGHTS RESERVED
//
// This file is the product of Bread Zeppelin and cannot be 
// reproduced, copied, or used in any shape or form without 
// the express written consent of Bread Zeppelin.
//************************************************************
//
//	$Id: Concurrent_Assg6
//
//	$Date: Feb 16, 2012
//
//	Reason: This is the task for checking and taking an order to the cook.
//	The main behavior of this task is defined below.
//
//*************************************************************

/** $Author: Ben Hare, Zach Huenink, Philipp Wittmaack, Peter Maresca */
/** $Revision: 1*/

// Default Package

// system imports

// project includes

//==============================================================

/**
 * 
 *
 */
public class CheckAndTakeOrder_Task implements Runnable {

	Restaurant restaurant = Restaurant.getInstance();
	Table table;
	final CustomerParty customerParty;

	/**
	 * Constructor for CheckAndTakeOrder task.
	 * @param customerParty
	 */
	CheckAndTakeOrder_Task(CustomerParty customerParty) {
		this.customerParty = customerParty;
	}

	@Override
	public void run() {
		
		Order order = customerParty.createOrder();
		
		int time = new Random(System.currentTimeMillis()).nextInt(60000);
		time = time + 60000;
		try {
			if(time > 110000){
				Thread.currentThread().interrupt();
			}
			Thread.sleep(time/Simulator.simulationSpeed);
			
			System.out.println("	" + customerParty.getName() + " has ordered!");
			
			// Finish WaitingOrderTime Metric
			RestaurantMetrics.getInstance().finishWaitingToOrderTime(customerParty, System.currentTimeMillis());
			// Begin FromOrderToDeliveryTime Metric
			RestaurantMetrics.getInstance().beginFromOrderToDeliveryTime(customerParty, System.currentTimeMillis());

			Preparation_Task preparationTask = new Preparation_Task(order);
			restaurant.getCookExecutor().execute(preparationTask);

		} 
		catch (InterruptedException e) {
			customerParty.rageQuit();
			System.out.println(customerParty.getName() + " has left the " +
			"premises after waiting too long to order.");
			
			Table t = customerParty.getCustomerTable();
			restaurant.returnATable(t);
		}
	}

}
